$(function() {
	$("#jqGrid").jqGrid({
		url : '../service/list',
		datatype : "json",
		colModel : [ {
			label : '服务编号',
			name : 'serviceId',
			width : 60,
			hidden : true,
			key : true
		}, {
			label : '服务名称',
			name : 'serviceName',
			width : 80
		}, {
			label : '服务标识名称',
			name : 'serviceCode',
			width : 80
		} ],
		viewrecords : true,
		height : 385,
		rowNum : 10,
		rowList : [ 10, 30, 50 ],
		rownumbers : true,
		rownumWidth : 25,
		autowidth : true,
		multiselect : true,
		pager : "#jqGridPager",
		jsonReader : {
			root : "page.list",
			page : "page.currPage",
			total : "page.totalPage",
			records : "page.totalCount"
		},
		prmNames : {
			page : "page",
			rows : "limit",
			order : "order"
		},
		gridComplete : function() {
			// 隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqgrid-bdiv").css({
				"overflow-x" : "hidden"
			});
		}
	});
});

//页面验证
var service;
$(function() {
	 service = $("#service").Validform({
		tiptype : 3,
		showAllError : true,
		datatype : {
			//匹配是否合法(字母开头，允许5-16字节，允许字母数字下划线)
			"serviceCode" : /^[a-zA-Z][a-zA-Z0-9_]{2,15}$/,
		},

	});
	 service.addRule([ {
		ele : ".form-control:eq(0)",
		datatype : "s2-16",
		nullmsg : "请输入服务名称",
		errormsg : "服务名称至少2个字符,最多16个字符！"
	}
	]);
});

var vm = new Vue({
	el : '#rrapp',
	data : {
		q : {
			serviceName : null
		},
		showList : true,
		title : null,
		service : {}
	},
	methods : {

		query : function() {

			vm.reload();
		},
		add : function() {

			vm.showList = false;
			vm.title = "新增";
			vm.service = {};
		},
		update : function(event) {
			var serviceId = getSelectedRow();
			if (serviceId == null) {
				return;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(serviceId);
		},
		saveOrUpdate : function(event) {
			//表单提交前验证
			var bool =service.check();
			if(!bool){
				return false;
			}
			
			console.log(vm.service, "-------*********");
			var url = vm.service.serviceId == null ? "../service/save"
					: "../service/update";
			$.ajax({
				type : "POST",
				url : url,
				data : JSON.stringify(vm.service),
				success : function(r) {
					if (r.code === 0) {
						alert('操作成功', function(index) {
							vm.reload();
						});
					} else {
						alert(r.msg);
					}
				}
			});
		},
		del : function(event) {
			var serviceIds = getSelectedRows();
			if (serviceIds == null) {
				return;
			}

			confirm('确定要删除选中的记录？', function() {
				console.log(serviceIds);
				$.ajax({
					type : "POST",
					url : "../service/delete",
					data : JSON.stringify(serviceIds),
					success : function(r) {
						if (r.code == 0) {
							alert('操作成功', function(index) {
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo : function(serviceId) {
			$.get("../service/info/" + serviceId, function(r) {
				vm.service = r.service;
			});

		},

		reload : function(event) {
			debugger;
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam', 'page');
			console.log(page);
			console.log(vm.q.serviceName, "-------*********");
			$("#jqGrid").jqGrid('setGridParam', {
				postData : {
					'serviceName' : vm.q.serviceName
				},
				page : page
			}).trigger("reloadGrid");

		}
	}
});